package com.zhang.bingmei_incomeexpenditure.controller.other;

import com.zhang.bingmei_incomeexpenditure.entity.Utils.IECommUtil;
import com.zhang.bingmei_incomeexpenditure.entity.commom.R;
import com.zhang.bingmei_incomeexpenditure.entity.ie.CodeCityNameAndYearVO;
import com.zhang.bingmei_incomeexpenditure.entity.ie.CodeNameVO;
import com.zhang.bingmei_incomeexpenditure.entity.ie.IncomeExpenditure;
import com.zhang.bingmei_incomeexpenditure.entity.ie.IncomeExpenditurePageDTO;
import com.zhang.bingmei_incomeexpenditure.entity.ie.selectNew.SelectDoubleResult;
import com.zhang.bingmei_incomeexpenditure.service.IncomeExpenditureService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zsj
 * @description
 * @createDate 2025/10/24 19:18
 * @Entity com.zhang.bingmei_incomeexpenditure.controller.other.entity.
 */
@Slf4j
@RestController
@RequestMapping("/test")
public class TestController {

    @Resource
    private IncomeExpenditureService incomeExpenditureService;

    //获取当前用户管辖市辖区
    @GetMapping("/getDistricts")
    public R<List<CodeNameVO>> getDistricts() {
        List<CodeNameVO> districts = incomeExpenditureService.getDistricts();
        return R.ok(districts, districts.size());
    }

    @PostMapping("/getAllCities")
    public R<List<CodeCityNameAndYearVO>> getAllCities(@RequestBody @Validated IncomeExpenditurePageDTO dto) {
        List<CodeCityNameAndYearVO> allCities = incomeExpenditureService.getAllDistrict(dto);
        // 处理可能的空集合
        allCities = allCities == null ? Collections.emptyList() : allCities;
        return R.ok(allCities, allCities.size());
    }

    @PostMapping("/getAllIEData")
    public R<List<IncomeExpenditure>> getAllIEData(@RequestBody @Validated IncomeExpenditurePageDTO dto) {
        SelectDoubleResult incomeExpenditureResult = incomeExpenditureService.getAllIEData(dto);
        Map<String, Object> result = new HashMap<>(2);
        result.put("dataList", incomeExpenditureResult);       // 原收支情况列表(含合计)

        Boolean updateFlag = IECommUtil.isUpdateButton();
        result.put("updateFlag", updateFlag);

        log.debug("getAllIEData(): 获取收支情况列表，出参：result:{}",result);
        int size = incomeExpenditureResult.getSelectDoubleResults().size();
        return R.ok(result,size);
    }

}
